package com.ybfenpan.controller.member.wealth;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.log.LogShui;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.log.LogShuiService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
 
public class BuyShuiController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogShuiService logShuiService;

	
	
	@Token(save=true)
	@RequestMapping("member/wealth/buyShuiShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);

		return "member/wealth/buyShui";
	}


	@RequestMapping("member/wealth/buyShui.shtml")
	@ResponseBody
	@Token(remove=true)
	public void buy(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception{
		
		Boolean result=true;
		String msg = "";
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());

		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
		Double num = MapUtil.getDoubleValue(formMap, "num");
    	if(!member.getTwoPassword().equals(pwd)){
    		msg = "安全密码不正确。";
    		result=false;
    	}else{
    		if (num<=0.0){
    			msg="输入购买数量不正确！";
    			result=false;
			}
    	}
		
    	if(result){

			LogShui logShui=new LogShui();
			logShui.setMemberId(member.getId());
			logShui.setNum(num);
			logShui.setTitle("已提交后台，等待管理员审核！");
			logShui.setState(10);
			logShui.setYu(member.getShui());
			logShuiService.saveOrUpdate(logShui);
    	}
    	
    	HashMap resultformMap = new HashMap();
		resultformMap.put("msg", msg);
		renderSuccess(response, result, resultformMap); 
		
	}
}
